package com.example.smartcitydemo.ui.smartbus.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.smartcity_jscs.network.ResultStates.Resource

import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.databinding.FragmentSmartBusBinding
import com.example.smartcitydemo.ui.smartbus.adapter.SmartBusExpanableAdapter
import com.example.smartcitydemo.viewModel.smartBus.SmartBusViewModel

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.smartbus.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/8/24 10:23
 * @Email: 1919796283@qq.com
 */
class SmartBusFragment : BaseFragment<FragmentSmartBusBinding>(R.layout.fragment_smart_bus) {
    private val smartBusViewModel: SmartBusViewModel by activityViewModels()
    private val exAdapter by lazy {
        SmartBusExpanableAdapter()
    }

    override fun addObservers(myViewBinding: FragmentSmartBusBinding) {
        smartBusViewModel.busLiveData.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                myViewBinding.myLoaddingView.visibility = View.GONE
                myViewBinding.exListSmartBus.visibility = View.VISIBLE
                exAdapter.list = it.data!!
                exAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView(myViewBinding: FragmentSmartBusBinding, savedInstanceState: Bundle?) {
        myViewBinding.run {
            myLoaddingView.visibility = View.VISIBLE
            toolbarSmartBus.tvContent.text="智慧巴士"
            exListSmartBus.run {
                visibility = View.GONE
                setAdapter(exAdapter)
                setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
                    smartBusViewModel.getChoiceData(groupPosition)
                    this@SmartBusFragment.findNavController().navigate(R.id.action_navigation_smart_bus_to_navigation_smart_bus_first)
                    true
                }
            }

        }

    }

    override fun initData() {
        smartBusViewModel.getBusExpanableData()
    }
}